/*
 * @Author: jingfuu@163.com
 * @Date: 2024-12-04 15:04:24
 * @LastEditTime: 2024-12-11 14:35:44
 * @Description: file content
 */
import {
  Controller,
  Delete,
  Get,
  Inject,
  Logger,
  Param,
  Post,
  Query,
  Res,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import LoggerService from '../service/LoggerService';
import SysLogininforDto from '../dto/SysLogininforDto';
import LogininforQueryParam from '../param/LogininforQueryParam';
import { R } from 'src/common/R';
import PageResult from 'src/common/PageResult';
import { Response } from 'express';
import ServiceException from 'src/common/ServiceException';
import SysOperLogQueryParam from '../param/SysOperLogQueryParam';
import SysOperLogDto from '../dto/SysOperLogDto';
import { PreAuthorize } from 'src/common/decorator/pre-authorize.decorator';

/*
 * @Author: jingfuu@163.com
 * @Date: 2024-12-04 15:04:24
 * @LastEditTime: 2024-12-05 10:52:45
 * @Description: 系统监控
 */
@ApiTags('系统监控')
@Controller('monitor')
class MonitorController {
  private readonly logger = new Logger(MonitorController.name);

  @Inject()
  private logService: LoggerService;

  @PreAuthorize('monitor:logininfor:unlock')
  @ApiOperation({ summary: '登录用户解锁' })
  @Get('/logininfor/unlock/:username')
  async unlockLogininfor(@Param() username: string): Promise<R<any>> {
    await this.logService.loginUserUnlock(username);
    return R.ok();
  }
  @PreAuthorize('monitor:logininfor:list')
  @ApiOperation({ summary: '分页查询登录日志' })
  @Get('/logininfor/list')
  async findLogininforList(
    @Query() param: LogininforQueryParam,
  ): Promise<R<SysLogininforDto>> {
    const result: PageResult<SysLogininforDto> =
      await this.logService.listLogininfor(param);
    return R.page(result);
  }

  @PreAuthorize('monitor:logininfor:remove')
  @ApiOperation({ summary: '删除登录日志' })
  @Delete('/logininfor/:id(\\d+)')
  async deleteLogininfor(@Param('id') id: string): Promise<R<any>> {
    if (id.indexOf(',') > -1) {
      for (const item of id.split(',')) {
        await this.logService.deleteLogininfor(Number.parseInt(item));
      }
    } else {
      await this.logService.deleteLogininfor(Number.parseInt(id));
    }
    return R.ok();
  }

  @PreAuthorize('monitor:logininfor:clean')
  @ApiOperation({ summary: '删除所有登录日志' })
  @Delete('/logininfor/clean')
  async cleanLogininfor(): Promise<R<any>> {
    await this.logService.cleanLogininfor();
    return R.ok();
  }

  @PreAuthorize('monitor:logininfor:export')
  @ApiOperation({ summary: '导出登录日志' })
  @Post('/logininfor/export')
  async exportLogininfor(
    @Query() param: LogininforQueryParam,
    @Res() res: Response,
  ) {
    try {
      const buffer = await this.logService.exportLogininfor(param);
      res.set({
        'Content-Type': 'application/vnd.ms-excel',
        'Content-Disposition': 'attachment; filename=logininfor.xlsx',
      });
      res.send(buffer);
    } catch (error) {
      this.logger.error('登录日志导出失败', error.stack);
      throw new ServiceException('登录日志导出失败');
    }
  }

  @PreAuthorize('monitor:operlog:list')
  @ApiOperation({ summary: '分页查询操作日志' })
  @Get('/operlog/list')
  async findOperlogList(
    @Query() param: SysOperLogQueryParam,
  ): Promise<R<SysOperLogDto>> {
    const result: PageResult<SysOperLogDto> =
      await this.logService.listOperLog(param);
    return R.page(result);
  }

  @ApiOperation({ summary: '删除操作日志' })
  @Delete('/operlog/:id(\\d+)')
  async deleteOperlog(@Param('id') id: string): Promise<R<any>> {
    if (id.indexOf(',') > -1) {
      for (const item of id.split(',')) {
        await this.logService.deleteOperlog(Number.parseInt(item));
      }
    } else {
      await this.logService.deleteOperlog(Number.parseInt(id));
    }
    return R.ok();
  }

  @PreAuthorize('monitor:operlog:clean')
  @ApiOperation({ summary: '删除所有操作日志' })
  @Delete('/operlog/clean')
  async cleanOperlog(): Promise<R<any>> {
    await this.logService.cleanOperlog();
    return R.ok();
  }

  @PreAuthorize('monitor:operlog:export')
  @ApiOperation({ summary: '导出操作日志' })
  @Post('/operlog/export')
  async exportOperlog(
    @Query() param: SysOperLogQueryParam,
    @Res() res: Response,
  ) {
    try {
      const buffer = await this.logService.exportOperlog(param);
      res.set({
        'Content-Type': 'application/vnd.ms-excel',
        'Content-Disposition': 'attachment; filename=operlog.xlsx',
      });
      res.send(buffer);
    } catch (error) {
      this.logger.error('操作日志导出失败', error.stack);
      throw new ServiceException('操作日志导出失败');
    }
  }
}
export default MonitorController;
